package com.eliza.music.view.music.settinng

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.KeyboardArrowRight
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Card
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter
import com.eliza.library.utils.compose.ImageBuild
import com.eliza.music.model.entity.BtnGroup
import com.eliza.music.model.tmp.ArrBtnGroup
import com.eliza.music.model.tmp.ResBackground
import com.eliza.music.ui.theme.PaddingSize


@Preview
@Composable
private fun Preview() {
    SettingX.Content()
}

object SettingX {
    @Composable
    fun Index(
        handle: @Composable (drawerState: DrawerState, modifier: Modifier) -> Unit,
    ) {
        val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
        ModalNavigationDrawer(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.LightGray),
            drawerState = drawerState,
            gesturesEnabled = true,
            drawerContent = {
                Content(Modifier.fillMaxWidth(0.9f))
            }) {
            handle(
                drawerState, Modifier.fillMaxSize()
            )
        }
    }

    @Composable
    fun Content(modifier: Modifier = Modifier) {
        LazyColumn(
            modifier = Modifier
                .fillMaxHeight()
                .then(modifier)
                .background(Color(245, 245, 245)),
            horizontalAlignment = Alignment.Start,
        ) {
            val fillmax = Modifier.fillMaxWidth()
            item {
                TitleBar(fillmax)
            }
            item {
                ViperAd(fillmax)
            }
            items(ArrBtnGroup) {
                Spacer(modifier = Modifier.height(PaddingSize.Normal.size))
                ContentCard(it, fillmax)
            }
        }
    }

    @Composable
    private fun TitleBar(modifier: Modifier = Modifier) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .height(70.dp)
                .padding(PaddingSize.Normal.size)
                .then(modifier),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            Row(
                modifier = Modifier.fillMaxHeight(),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Image(
                    painter = rememberAsyncImagePainter(
                        model = ImageBuild.CoilImageRequest(
                            LocalContext.current, ResBackground.random()
                        )
                    ),
                    contentDescription = "",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .fillMaxHeight()
                        .aspectRatio(1f)
                        .clip(CircleShape)
                        .border(2.dp, Color.White, CircleShape),
                )
                Text(
                    "小萌新在此", modifier = Modifier.padding(PaddingSize.Normal.size)
                )
                Icon(
                    imageVector = Icons.AutoMirrored.Rounded.KeyboardArrowRight,
                    contentDescription = ""
                )
            }
            Icon(
                imageVector = Icons.Rounded.Search,
                contentDescription = "",
                modifier = Modifier.fillMaxHeight(),
            )


        }
    }

    @Composable
    private fun ContentCard(data: BtnGroup, modifier: Modifier = Modifier) {
        val fontSize = 18.sp
        Column(
            modifier = Modifier
                .then(modifier)
                .background(Color.White, RoundedCornerShape(20.dp))
                .padding(PaddingSize.Normal.size),
            verticalArrangement = Arrangement.Center
        ) {
            if (data.title.isNotEmpty()) {
                Text(
                    text = data.title,
                    fontSize = 18.sp,
                    color = Color.Gray,
                    fontWeight = FontWeight.W500,
                    modifier = Modifier.then(modifier)
                )
                HorizontalDivider(
                    modifier = Modifier
                        .height(1.dp)
                        .background(Color(254, 254, 254))
                )
            }
            data.children.forEach {
                val press = remember {
                    mutableStateOf(false)
                }
                val backgroundColor by animateColorAsState(
                    if (press.value) Color.Gray else Color.White,
                    label = ""
                )
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(40.dp)
                        .background(backgroundColor, RoundedCornerShape(20.dp))
                        .clickable {
                        },
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        Icon(
                            imageVector = it.icon,
                            contentDescription = "",
                            modifier = Modifier
                                .fillMaxHeight()
                                .padding(PaddingSize.Normal.size, PaddingSize.Normal.size)
                        )
                        Text(
                            it.title,
                            fontWeight = FontWeight.W500,
                            fontSize = fontSize,
                            fontFamily = FontFamily.SansSerif
                        )
                    }
                    it.content(modifier)
                }
            }

        }
    }


    @Composable
    private fun ViperAd(modifier: Modifier = Modifier) {
        Card(
            modifier = Modifier
                .height(150.dp)
                .padding(PaddingSize.Normal.size)
                .then(modifier),
            shape = RoundedCornerShape(20.dp)
        ) {
            AsyncImage(
                model = ImageBuild.CoilImageRequest(
                    LocalContext.current,
                    ResBackground.random()
                ),
                contentDescription = "",
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxWidth()
            )
            Text(text = "会员已过期")
            Text(text = "重启会员成长之路")
            Text(text = "会员周年庆 VIP 只需六折")
        }
    }
}
